<<<<<<< HEAD
package com.wyz.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyz.dto.UserInfoDto;
import com.wyz.entity.UserEntity;
import com.wyz.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.security.SecureRandom;


/**
 * @author wsf
 * @desc
 * @date 2024/12/23 11:40
 */
@Service
@Slf4j
public class UserService{
    @Resource
    private UserMapper userMapper;
    /**
     * 用户注册
     * @return ture 注册成功 false 注册失败
     */
    public boolean register(UserEntity userEntity) {
        // 判断用户名是否存在
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", userEntity.getUserAccount());
        UserEntity  userEntity1 = userMapper.selectOne(queryWrapper);
        if (userEntity1 != null) {
            log.info("用户名已存在");
            return false;
        }
        // 保存用户注册信息
        userEntity1 = new UserEntity();
        BeanUtils.copyProperties(userEntity, userEntity1);
        int result = userMapper.insert(userEntity1);
        return result > 0 ? true : false;
    }
    /**
     * 用户登录
     * @param
     * @return ture 登录成功 false 登录失败
     *

     */
    public boolean login(String userAccount, String UserPassword) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", userAccount);
        UserEntity userEntity = userMapper.selectOne(queryWrapper);
        // 验证用户是否存在
        if (userEntity == null) {
            System.out.println("用户名不存在");
            return false;
        }

        // 验证密码
        if (!userEntity.getUserPassword().equals(UserPassword)) {
            System.out.println("密码错误");
            return false;
        }
        return true; // 登录成功
    }

    /**邮箱验证重新设置密码
     * @return ture 密码重置成功 false 密码重置失败
     */
    @Resource
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    // 发送重置密码邮件
    @Resource
    private MailService mailService;
    @Transactional
    public boolean sendResetPasswordEmail(String userAccount){
        UserEntity user = userMapper.findByUserAccount(userAccount);
        if (user == null) {
            return false;  // 用户不存在
        }
        //随机生成一个六位数的密码
        String newPassword = generateRandomPassword();
        user.setUserPassword(newPassword);//数据库更新密码
        userMapper.updateByUserEmail(user);
        // 发送新密码邮件
        String subject = "您的新密码";
        String body = "您的新密码是： " + newPassword + "\n请登录后及时修改密码。";
        mailService.sendEmail(user.getUserEmail(), subject, body);

        return true;
    }

    private String generateRandomPassword() {
        // 使用 SecureRandom 生成随机密码
        SecureRandom random = new SecureRandom();
        int password = 100000 + random.nextInt(900000);
        return String.valueOf(password);  // 将其转为字符串
    }
    public boolean updateUserInfo(Integer userId, UserInfoDto userInfoDto){
        // 检查用户ID是否合法
        if(userId==null||userId<=0){
            throw new IllegalArgumentException("用户ID不合法");
        }

        // 可以在这里进行一些业务逻辑校验
        // 比如用户信息的合法性验证等
        UserEntity userEntity=new UserEntity();
        // 将DTO转换为Entity
        userEntity.setUserId(userId);
        userEntity.setUserAccount(userInfoDto.getUserAccount());
        userEntity.setUserEmail(userInfoDto.getUserEmail());
        // 可以添加其他需要更新的字段

        // 调用Mapper进行更新操作
        int rowsAffected=userMapper.updateById(userEntity);
        return rowsAffected>0;  // 如果受影响的行数大于0则返回true
    }

    public UserEntity selectUserByAccount(String account) {
        return userMapper.selectUserByUserAccount(account);
    }
}
=======
package com.wyz.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyz.entity.StudentEntity;
import com.wyz.entity.UserEntity;
import com.wyz.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wk
 * @desc
 * @date 2024/12/23 11:40
 */
@Service
public class UserService{
    @Resource
    private UserMapper userMapper;

}
>>>>>>> be516e15a35375b38cf973b1ce88c92dd1ceba03
